//
//  NSAttributedString+Extension.swift
//  Weibo
//
//  Created by 黄亦寒 on 2016/10/8.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit

extension NSAttributedString {
    //根据表情模型生成富文本对象
    static func attributedStringWithEmoticon(emoticon:HMEmoticon,font:UIFont)-> NSAttributedString {
        //根据表情路径创建UIImage对象
        let image = UIImage(named:emoticon.path!)
        //根据image对象创建文本附件
        let attachment = HMTextAttachment()
        //设置文本附件对应的表情模型
        attachment.emoticon = emoticon
        //设置图片大小
        //获取字体高度
        let fontHeight = font.lineHeight
        //通过设置文本附件的bounds调整表情的显示位置
        attachment.bounds = CGRect(x: 0, y: -4, width: fontHeight, height: fontHeight)
        attachment.image = image
        //根据文本附件创建符文富文本
        let attributedStr = NSAttributedString(attachment: attachment)
        return attributedStr
    }
}
